<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends UserController {
	//显示商品列表
    public function index(){
        //创建对象
        $goods = M('goods');

       //获取每页显示的数量
       $num = !empty($_GET['num']) ? $_GET['num'] : 2;

        //获取关键字
        if(!empty($_GET['keyword'])){
            //有关键字
            $where = "goodsname like '%".$_GET['keyword']."%'";
        }else{
            $where = '';
        }


        // 查询满足要求的总记录数
        $count = $goods->where($where)->count();
        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,$num);
        //获取limit参数
        $limit = $Page->firstRow.','.$Page->listRows;

         //执行查询
        $goodss = $goods->where($where)->limit($limit)->select();
        // echo '<pre>';
        //     print_r($goodss);
        // echo '</pre>';
        // 分页显示输出
        $pages = $Page->show();
        
        //分配变量
        $this->assign('goodss',$goodss);
        $this->assign('pages',$pages);
    	//解析模板
    	$this->display();
    }

    //显示商品的添加页面
    public function add(){
        
        //创建对象
        $cate = M('cate');
        //查询所有分类

         //执行查询
        $cates = $cate->order('concat(path,id) asc')->select();

        //遍历
        foreach ($cates as $k => $v) {
            //获取要添加|---个数
            $c = count(explode(',',$v['path']))-2;
            $cates[$k]['name'] = str_repeat('|----',$c).$v['name'];

        }
       
        //分配变量
        $this->assign('cates',$cates);


    	//解析模板
    	$this->display();
    }

    //处理商品的数据添加
    public function insert(){
        //调用函数处理图片上传

        // var_dump($_POST);die;
        $date = $_POST;
        $date['time'] = date('Y-m-d H:i:s');
        $date['sale'] = 0;

        //创建表对象
        // $goods = M('goods');
        $goods = M('goods');
        //创建数据
        
        if(!$goods->create($date)){
            //获取错误信息
            $info = $goods->getError();
            $this->error($info);
        }
        
       
        // var_dump($res);
        //执行添加
        $id = $goods->add();
       if($id){
             //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            // $this->success('新增成功', U('Admin/goods/index'),3);
       }else{
            //错误页面的默认跳转页面是返回前一页，通常不需要设置
            $this->error('新增数据失败');
       }
    
       if($_FILES['pic']['error'] == 0){
           $upload = new \Think\Upload();// 实例化上传类    
           $upload->maxSize   =     3145728 ;// 设置附件上传大小    
           $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
           $upload->rootPath  =      './Public';
           $upload->savePath  =      '/GoodsUploads/'; // 设置附件上传目录   
           // 上传文件     
           $info   =   $upload->upload();    
           if(!$info) {// 上传错误提示错误信息       
               $this->error($upload->getError());    
           }else{// 上传成功        
               // $this->success('上传成功！'); 
               $str =  ltrim($upload->rootPath,'.').$info['gimg']['savepath'].$info['gimg']['savename'];
           }
       }

       $gimg = M('gimg');

       $gimg->create();
       $gimg->gid = $id;
       $gimg->src = $str;
       $gimg->ispage = 0; 
       if($gimg->add()){
            $this->success('新增成功', U('Admin/goods/index'),3);
       }else{
            $this->error('上传图片失败');
       }



    }

    //商品的修改
    public function save(){
                //创建对象
        //var_dump($_GET);
        $id = $_GET[id];
        
        $cate = M('cate');
        //查询所有分类
        
        //执行查询
        $cates = $cate->order('concat(path,id) asc')->select();
        // var_dump($cates);
        
        $goods = M('goods');
        $goodss = $goods->where('id='.$id)->field('cid')->select();
        
        $cid = $goodss[0]['cid'];
        $cname=$cate->where('id='.$cid)->field('name')->select();
        $cname = $cname[0]['name'];

        $this->assign('cates',$cates);//所有类的数组
        $this->assign('cname',$cname);//类名
        
        $goodr = $goods->where('id='.$id)->select();
        $goodr = $goodr[0];
        // var_dump($goodr);die;
        $this->assign('goods',$goodr);//goods表的一维数组
        
        //查询图片
        $gimg = M('gimg');
        $gimgs = $gimg->where('gid='.$id)->select();
        // var_dump($gimgs);die;
        $this->assign('gimg',$gimgs);
        $res = htmlspecialchars_decode($goodr['porfile']);
        $this->assign('res',$res);
        $this->assign('gid',$id);

        // echo '<pre>';
        //     print_r($gimgs);
        // echo '</pre>';die;
        $this->display();
        
    }

    //执行修改
    public function update(){
        // var_dump($_POST);
         //创建数据表对象
        $goods = M('goods');
       // echo '<pre>';
       //     print_r($_POST);
       // echo '</pre>';die;
        //创建数据
        $res = $goods->create();
        //执行修改
       $res =  $goods->save();
       // var_dump($res);die;
        //执行添加
       if($res){
             //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            $this->success('更新成功', U('Admin/goods/index'),3);
       }else{
            //错误页面的默认跳转页面是返回前一页，通常不需要设置
            $this->error('更新失败');
       }
    }


    public function ajaxdel(){
        // var_dump($_GET);
        //创建表对象
        $gimg = M('gimg');
        //获取id
        $id = $_GET['id'];
        var_dump($_GET);
        //查询表内所有图片
        $gimgs = $gimg->where('id ='.$id)->field('gid')->select();
        // var_dump($gimgs);die;
        var_dump($gimgs);
        $gid = $gimgs[0]['gid'];
        $date['ispage'] = 0;
        $gimgs = $gimg->where('gid='.$gid)->save($date);
        if ($gimgs) {
            echo '成功';
        }else{
            echo '失败';
        }
        $date['ispage'] = 1;
        $gimga = $gimg->where('id='.$id)->save($date);
        if ($gimga) {
            echo '成功';
        }else{
            echo '失败';
        }
        // 向ajax返回数据
        // if($res){
        //     echo 1;
        // }else{
        //     echo 0;
        // }
    }
//删除图片
    public function ajaxdel1(){
        // var_dump($_GET);
        //创建表对象
        $gimg = M('gimg');
        //获取id
        $id = $_GET['id'];
        var_dump($id);
        $gimgs = $gimg->where('id ='.$id)->field('gid')->select();
        var_dump($gimgs);
        $gid = $gimgs[0]['gid'];
        $num = $gimg->where('gid='.$gid)->count();
        // var_dump($gimgs);
        if($num>1){
            $gimgss = $gimg->where('id='.$id)->delete();
            return 1;
        }else{
            return flase;
        }
        
    }

    public function addimg(){
        // var_dump($_GET);
        //创建表对象
        // echo '我是修改图片界面';
        // echo '<pre>';
        //     print_r($_GET);
        // echo '</pre>';
        $id=$_GET['id'];
       
        $this->assign('id',$id);
        $this->display();
    }



    public function imginsert(){
        // var_dump($_GET);
        //创建表对象
        // echo '我是图片修改界面';
        // echo '<pre>';
        //     print_r($_POST);
        // echo '</pre>';
        // echo '<pre>';
        //     print_r($_FILES);
        // echo '</pre>';
       
     
        if($_FILES['gimg']['error'] == 0){
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->rootPath  =       './Public';
            $upload->savePath  =      '/Uploads/'; // 设置附件上传目录   
            // 上传文件     
            $info   =   $upload->upload();    
            if(!$info) {// 上传错误提示错误信息       
                $this->error($upload->getError());    
            }else{// 上传成功        
                // $this->success('上传成功！'); 
                $str =  ltrim($upload->rootPath,'.').$info['gimg']['savepath'].$info['gimg']['savename'];
            }

         $gimg = M('gimg');
         // echo '<pre>';
         //     print_r($_POST);
         // echo '</pre>';
         $id = $_POST['id'];
         $date['gid']=$id;
         $date['src']=$str;
         // echo '<pre>';
         //     print_r($date);
         // echo '</pre>';die();
         $aaa=$gimg->add($date);
         if($aaa){
                     //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                    $this->success('添加成功', U('Admin/goods/save/id/'.$id),3);
               }else{
                    //错误页面的默认跳转页面是返回前一页，通常不需要设置
                    $this->error('添加失败');
               }
            }

    }
}
